﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DeleteLog
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void WatingTask<T>(Func<T> func, Action<T> action)
        {
            this.label1.Visible = true;
            //action(await task());
            func.BeginInvoke(new AsyncCallback(r =>
            {
                var t = func.EndInvoke(r);
                this.Invoke(new Action<T>(p =>
                {
                    action(p);
                    this.label1.Visible = false;
                }), t);


            }), null);

        }

        List<string> files = new List<string>();
        private void button1_Click(object sender, EventArgs e)
        {
            WatingTask(SearchPatternFiles, p => { this.richTextBox1.Text = p; this.Text = files.Count.ToString(); });
        }
        private string SearchPatternFiles()
        {
            //aic20140102_11_04.log
            files.Clear();
            var path = Environment.CurrentDirectory;
            files.AddRange(Directory.EnumerateFiles(path, "aic*", SearchOption.TopDirectoryOnly));
            files.AddRange(Directory.EnumerateFiles(path, "log*", SearchOption.TopDirectoryOnly));
            files.AddRange(Directory.EnumerateFiles(path, "smf*", SearchOption.TopDirectoryOnly));
            StringBuilder sb = new StringBuilder();
            files.ForEach(p =>
            {
                sb.AppendLine(p);
            });
            return sb.ToString();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (files == null)
                return;
            for (int i = 0; i < files.Count; i++)
            {
                try
                {
                    File.Delete(files[i]);
                }
                catch (Exception)
                {
                }
                this.Text = "                   " + i + "/" + files.Count;
            }
        }

        //hostroy
        List<string> historyDries = new List<string>();
        private void button3_Click(object sender, EventArgs e)
        {
            historyDries.Clear();
            var path = Environment.CurrentDirectory;
            var dires = Directory.EnumerateDirectories(path, "2013*", SearchOption.TopDirectoryOnly);
            this.richTextBox1.Clear();
            foreach (var item in dires)
            {
                historyDries.Add(item);
                this.richTextBox1.AppendText(item);
                this.richTextBox1.AppendText(Environment.NewLine);
            }
            //2014-02
            dires = Directory.EnumerateDirectories(path, "2014*", SearchOption.TopDirectoryOnly);
            foreach (var item in dires)
            {
                var direName = Path.GetFileName(item);
                if (direName.IndexOf('-') > 0)
                {
                    var month = int.Parse(direName.Substring(5, 2));
                    if (month < DateTime.Now.Month - 1)
                    {
                        historyDries.Add(item);
                        this.richTextBox1.AppendText(item);
                        this.richTextBox1.AppendText(Environment.NewLine);
                    }
                }
                else
                {
                    foreach (var item2 in Directory.EnumerateDirectories(item))
                    {
                        if (int.Parse(Path.GetFileName(item2)) < DateTime.Now.Month - 1)
                        {
                            historyDries.Add(item2);
                            this.richTextBox1.AppendText(item2);
                            this.richTextBox1.AppendText(Environment.NewLine);
                        }
                    }
                }

            }

        }
        private void button5_Click(object sender, EventArgs e)
        {
            foreach (var item in historyDries)
            {
                Directory.Delete(item, true);
            }
        }

        List<string> historyFiles = new List<string>();
        private void button4_Click(object sender, EventArgs e)
        {
            WatingTask(SearchPatternDateFiles, p => { this.richTextBox1.Text = p; this.Text = historyFiles.Count.ToString(); });
        }
        private string SearchPatternDateFiles()
        {
            historyFiles.Clear();
            var path = Environment.CurrentDirectory;
            var files = Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories);

            foreach (var item in files)
            {
                var lasttime = File.GetLastWriteTime(item);
                var extName = Path.GetExtension(item);
                if (lasttime.AddDays(45) < DateTime.Now && extName != ".aspx")
                {
                    historyFiles.Add(item);
                }
            }
            files = new List<string>();
            DirectoryInfo robotDire = new DirectoryInfo(path);
            var robotPath = Path.Combine(robotDire.Parent.FullName, "Robot", "Robotlog");
            var settdumpPath = Path.Combine(robotDire.Parent.FullName, "SettingDump");
            if (Directory.Exists(robotPath))
                files = Directory.EnumerateFiles(robotPath, "*", SearchOption.AllDirectories);
            if (Directory.Exists(settdumpPath))
            {
                var settDumpFiles = Directory.EnumerateFiles(settdumpPath, "*", SearchOption.AllDirectories);
                files = files.Union(settDumpFiles);
            }
            foreach (var item in files)
            {
                var lasttime = File.GetLastWriteTime(item);
                if (lasttime.AddDays(15) < DateTime.Now)
                {
                    historyFiles.Add(item);
                }
            }
            StringBuilder sb = new StringBuilder();
            historyFiles.ForEach(p =>
            {
                sb.AppendLine(p);
            });
            return sb.ToString();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < historyFiles.Count; i++)
            {
                try
                {
                    File.Delete(historyFiles[i]);
                }
                catch (Exception)
                {
                }
                this.Text = "                   " + i + "/" + historyFiles.Count;
            }
        }


    }
}
